/**
 *	Monopolymorphism - A Java-based monopoly!
 *
 *	The final project of 
 *	Object-Oriented Programming Design of NTUCSIE
 *
 *	Authors:
 *		ChungKingSing.
 *		Qing-Cheng Li.
 */
package monopolymorphism;

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;

public class Map{
	private boolean inited;
	private Scanner fin;
	private Scanner jin = new Scanner(System.in);
	private MapFactory mf;
	protected ArrayList<Player> players;
	protected ArrayList<Land> lands;
	protected String mapName;
	
	public Map(MapFactory _mf){
		mf = _mf;
		inited = false;
	}
	
	public Map(){
		mf = null;
		inited = false;
	}
	
	public void init(){
		makePlayer();
		mapName = selectMap();
		buildMap();
		inited = true;
	}
	
	public void initgui(gameSetting set,JProgressBar pb){
		makeGuiPlayer(set);
		pb.setValue(33);
		mapName = set.map;
		pb.setValue(50);
		lands = MapFactory.buildLand(mapName);
		pb.setValue(67);
		System.out.println("Map OK");
		inited = true;
		pb.setValue(100);
	}
	
	private void makeGuiPlayer(gameSetting set){
		players = new ArrayList<Player>();
		Player tmpPlayer;
		for(int i=0;i<set.player;i++){
			System.out.println("Build player:"+set.name[i]);
			tmpPlayer= new Player(i,set.name[i],set.money,false,0,new ArrayList<Property>(),new ArrayList<Card>(),false,false,0);
			players.add(tmpPlayer);
		}
		System.out.println("Player OK");
	}
	
	private void makePlayer(){
		/*
			Maybe here can ask use to set something...
			Default: 2 players.
		*/
		players = new ArrayList<Player>();
		Player tmpPlayer;
		tmpPlayer = new Player(0,"Player One",150000,false,0,new ArrayList<Property>(),new ArrayList<Card>(),false,false,0);
		players.add(tmpPlayer);
		tmpPlayer = new Player(1,"Player Two",150000,false,0,new ArrayList<Property>(),new ArrayList<Card>(),false,false,0);
		players.add(tmpPlayer);
		
		System.out.println("Two Player Add into this Map...");
	}
	
	private String selectMap(){
		/*
			可在此處實做選擇地圖的方式！
		*/
		System.out.println("Select the NTU Map");
		return "NTU";
	}
	
	private void buildMap(){
		lands = mf.buildLand(mapName);
		//System.out.println(lands.size());
	}
	
	/* This is just a test! */
	/*
	public void showMap(){
		System.out.println("Name = "+mapName);
		System.out.println("Size = "+lands.size());
		Land tmpLand;
		for(int i=0;i<40;i++){
			tmpLand = lands.get(i);
			System.out.println(tmpLand.id);
		}	
		
	}*/
	
	public void play(){
		Player tmpPlayer;
		Land tmpLand;
		Dice dice = new Dice();
		int diceNum;
		boolean saved;
		
		System.out.println("Start Monopolymorphism!");
		//showMap();
		do{
			System.out.println("New Round!");
			//for each player:
			for(int i=0;i<players.size();i++){
				tmpPlayer = players.get(i);
				System.out.println("Player:"+tmpPlayer.getName());
				
				if(tmpPlayer.isBankruptcy)
					continue;
				
				//check if in the jail
				if(tmpPlayer.inJail){
					if(tmpPlayer.inJailDay>1){
						tmpPlayer.inJailDay--;
					}else{
						tmpPlayer.inJailDay--;
						tmpPlayer.inJail = false;
					}
					continue;
				}
				
				//roll;
				diceNum = dice.roll();
				System.out.println(tmpPlayer.getName()+","+diceNum);
				
				//move
				System.out.print("move from "+tmpPlayer.location+" to ");
				if(!tmpPlayer.reverse)
					tmpPlayer.location = (tmpPlayer.location+diceNum+40)%40;
				else
					tmpPlayer.location = (tmpPlayer.location-diceNum+40)%40;
				System.out.println(tmpPlayer.location);
				
				//action
				if(lands.get(tmpPlayer.location).type=='C'){
					//random select a player
					//之後實做
					lands.get(tmpPlayer.location).action(tmpPlayer);
				}else{
					lands.get(tmpPlayer.location).action(tmpPlayer);
				}
				
				//switch?
				System.out.println("Please select:\n(1)build a house\n(2)do nothing\n(3)save\n(4)quit");
				saved = false;
				do{
					switch(jin.nextInt()){
						case 1:	//Build a house
							tmpPlayer.build();
							saved = false;
							break;
						case 2:	//Do Nothing.
							saved = false;
							break;
						case 3:	//Save Game
							System.out.println("Starting save game...");
							saved = true;
							//saveGame();
							System.out.println("Game saved!");
							break;
						case 4:	//Quit
							System.out.println("Quit this game");
							saved = false;
							return;
						/*
							土地買賣？順序或結構需要更動！
						*/
						default:
							saved = false;
							break;
					}
				}while(saved);
				System.out.println("=========\n"+tmpPlayer.getName()+"\nMoney="+tmpPlayer.getMoney()+"\n=========\n");
				
			}
		
		}while(!isEnd());
		
	}
	
	private boolean isEnd(){
		Player tmpPlayer;
		int nbp = 0;
		for(int i=0;i<players.size();i++){
			tmpPlayer = players.get(i);
			if(!tmpPlayer.isBankruptcy)
				nbp++;
		}
		//System.out.println("isEnd?=>"+nbp);
		if(nbp==1)
			return true;
		else
			return false;
	}
	
}
